<?php
namespace Admin\Controller;
use Common\Controller\BackendController;

class DirectController extends BackendController {
	
	public function _initialize() {
		parent :: _initialize();
		$this -> _cate_mod = D('ItemsCate');

		$api_config = M('ItemsSite')->where(array('code' => 'itaoke'))->getField('config');
		$this->_tbconfig = unserialize($api_config);
		
	} 
	public function index() {

		$this -> display();
	} 
	public function setting() {
		if (IS_POST) {
			$bl_45 = $this -> _post('cate_id', 'trim');
			$bl_47 = $this -> _post('fid', 'trim');
			$bl_48 = $this -> _post('eid', 'trim');
			$bl_50 = $this -> _post('applyreason', 'trim');
			$bl_51 = $this -> _post('direct_type', 'trim');
			if (!$bl_50) {
				$this -> ajaxReturn(0, '申请理由必须填写!');
			} 
			F('direct_setting', array('cate_id' => $bl_45, 'fid' => $bl_47, 'eid' => $bl_48, 'applyreason' => $bl_50,'direct_type'=>$bl_51));
			$this->collect();
		} 
	} 
	public function collect() {
		if (false === ($bl_49 = F('direct_setting'))) {
			$this -> ajaxReturn(0, L('illegal_parameters'));
		} 
		if (false === ($bl_44 = F('totalcoll'))) {
			$bl_44 = 0;
		} 
		if (false === ($bl_43 = F('nocoll'))) {
			$bl_43 = 0;
		} 
		if (false === ($bl_38 = F('okcoll'))) {
			$bl_38 = 0;
		} 
		if (false === ($queqiaocoll = F('queqiaocoll'))) {
			$queqiaocoll = 0;
		}		
		if (false === ($directcoll = F('directcoll'))) {
			$directcoll = 0;
		}
		if (false === ($failcoll = F('failcoll'))) {
			$failcoll = 0;
		}	
		if (false === ($firstcoll = F('firstcoll'))) {
			$firstcoll = 0;
		}					
		$bl_50 = $bl_49['applyreason'];
		$bl_37 = $bl_49['cate_id'];
		$this -> item_mod = M('Items');
		$bl_36 = $this -> _get('p', 'intval', 1);
		$bl_47 = $bl_49['fid'];
		$bl_48 = $bl_49['eid'];
		if ($bl_47 && $bl_48) {
			if($bl_48 !=0)
				$bl_39['id'] = array(array('egt', $bl_47), array('elt', $bl_48));
		} 
		
		$bl_39['shop_type'] = array('neq', 'M');
		if ($bl_37) {
			$bl_39['cate_id'] = $bl_37;
		} 
		$bl_39['pass'] = '1';
		$bl_39['ding'] = 0;
		$bl_39['coupon_end_time']= array('EGT',time());					 
		$bl_39['status'] ='underway';
		
	//	$bl_39['que'] = "0";
		$bl_40 = 1;
	//	$bl_42 = ($bl_36 - 1) * $bl_40;
		$bl_42=0;
		$bl_41 = $this ->item_mod->where($bl_39)->count('id');

		if ($bl_41==0) {
			$this -> ajaxReturn(0, '您设置的条件下没有数据，请重新设置或清除缓存！');
		}
		if($firstcoll==0){
			$firstcoll=$bl_41;
			F('firstcoll', $firstcoll);
		}
		$bl_51 = 'id asc ';
		$bl_52 = $this->item_mod->field('num_iid')->where($bl_39)->order($bl_51)->limit($bl_42, $bl_40)->select();
		if(empty($bl_52)){
			F('totalcoll', null);
			F('okcoll', null);
			F('nocoll', null);
			F('queqiaocoll', null);
			F('directcoll', null);
			F('failcoll', null);
			$this -> ajaxReturn(0, '申请完成！');			
		}
		if($bl_49['direct_type'] == 1){
			$params=array(
					'numiid'=>$bl_52[0]['num_iid'],
					'cookie'=>C('itk_alimama_cookie'),
					'siteid'=>C('itk_alimama_siteid'),
					'adzoneid'=>C('itk_queqiao_adzoneid'),
					'reason'=>$bl_50,
			);
			$local=A('Directlocal');
			$resp=$local->index($params);
		}else{
			// 调用api接口
			$tb_top = $this->_get_top();
			$req = $tb_top->load_api('ItaokeDirectApplyRequest');
			$req->setFields($bl_52[0]);											// 设置采集数据ID
			$req->setCookie(C('itk_alimama_cookie'));							// 设置cookie
			$req->putOtherTextParam('siteid',C('itk_alimama_siteid'));			// 设置siteid
			$req->putOtherTextParam('adzoneid',C('itk_queqiao_adzoneid'));		// 设置adzoneid
			$req->putOtherTextParam('reason',$bl_50);							// 设置申请reason
			$resp = (array)$tb_top->execute($req);			
		}

		if($resp['status'] != '0000')
		{
			$this->ajaxReturn(0, $resp['msg']);
		}
		else
		{
			if($resp['data']['isDone']== -2){
				F('totalcoll', null);
				F('okcoll', null);
				F('nocoll', null);
				F('queqiaocoll', null);
				F('directcoll', null);
				F('failcoll', null);
				$this -> ajaxReturn(0, '后妈登录失败,请重新配置COOKIE');
			}

			if($resp['data']['isDone']==1||$resp['data']['isDone']==2)	//定向和鹊桥
				$bl_38++;
			elseif($resp['data']['isDone']==0) // 通用
				$bl_43++;

			if($resp['data']['isDone']==2)		//鹊桥
				$queqiaocoll++;
			if($resp['data']['isDone']==1)		//定向
				$directcoll++;
			if($resp['data']['isDone']==-1)		//失败个数
				$failcoll++;
									
			if ($resp['data']['click_url'] && $resp['data']['isDone'] !=-1 && $resp['data']['isDone'] !=-2) {
				$_d['click_url']=$resp['data']['click_url'];
				$_d['ding']=1;
				$this->item_mod->where($bl_52[0])->save($_d);
			}
			$bl_44++;
		}

		F('totalcoll', $bl_44);
		F('okcoll', $bl_38);		// 成功个数
		F('nocoll', $bl_43);		// 跳过个数
		F('queqiaocoll', $queqiaocoll);		// 鹊桥个数
		F('directcoll', $directcoll);		// 鹊桥个数
		F('failcoll', $failcoll);
		
		
		if ($bl_44 >= $firstcoll) {
			F('totalcoll', null);
			F('okcoll', null);
			F('nocoll', null);
			F('queqiaocoll', null);
			F('directcoll', null);
			F('failcoll', null);			
			$this -> ajaxReturn(0, '申请完成！');
		} 
		$bl_25['title'] = '申请定向计划- 爱淘客联盟开发';
		$this -> assign('CheckItemCount', $firstcoll);
		$this -> assign('totalcoll', $bl_44);
		$this -> assign('okcoll', $bl_38);
		$this -> assign('nocoll', $bl_43);
		$this -> assign('queqiaocoll', $queqiaocoll);
		$this -> assign('directcoll', $directcoll);
		$this -> assign('failcoll', $failcoll);
				
		$bl_24 = $this->fetch('info');
		$this -> ajaxReturn(1, $bl_25, $bl_24);
	} 
	
	private function _get_top() {
		vendor('Itaoke.TopClient');
		vendor('Itaoke.RequestCheckUtil');
		vendor('Itaoke.Logger');
		$top = new \TopClient;
		$top->appkey = $this->_tbconfig['app_key'];
		$top->secretKey = $this->_tbconfig['app_secret'];
		return $top;
	}
} 
